import jpcap.*;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;

/**
 * Project: SecuritySnort
 * Write a sentence here.
 *
 * @author Jonathan Buttner
 * @version Jan 20, 2010
 */
public class DeviceCmd extends Command
{
	/**************************************************************************
	 * 							Globals
	 *************************************************************************/
	private NetworkInterface[] _devices;
	private NetworkInterface _preferredDev;
	
	/**************************************************************************
	 * 							Constructors
	 *************************************************************************/
	/**
	 * This is the default constructor to set up the Device Command.
	 */
	public DeviceCmd()
	{
		_name = "pickdevice";
		_devices = JpcapCaptor.getDeviceList();
		_preferredDev = null;
	}
	
	/**************************************************************************
	 * 							Public Methods
	 *************************************************************************/
	/**
	 * This method will get the devices and determine which the user
	 * wants to use.
	 * 
	 * @param args is an array of strings that were specified by the user.
	 * NOTE: it's basically not need at the moment.
	 */
	public void exeCommand( ArrayList < String > args )
	{
		_preferredDev = _exeDevices();
	}
	
	
	/**************************************************************************
	 * 							Getters/ Setters
	 *************************************************************************/
	/**
	 * This method returns the preferred device.
	 * 
	 * @return a NetworkInterface object the user wants to use.
	 */
	public NetworkInterface getPreDiv()
	{
		return _preferredDev;
	}
		
	/**************************************************************************
	 * 							Private Methods
	 *************************************************************************/
	/**
	 * This method will run the ShowDevice command.
	 * It takes care of find all the available and compatible
	 * devices that can be used.
	 * 
	 * @return the NetworkInterface device to connect to.
	 */
	private NetworkInterface _exeDevices()
	{
		//NetworkInterface[] devices = JpcapCaptor.getDeviceList();
		
		//Loops until all the devices are
		//printed with associated info with them
		for ( int i = 0; i < _devices.length; i++ )
		{
			System.out.println( i + " :" + _devices[i].name + "("
					+ _devices[i].description+")" );
			System.out.println( "    data link:" + _devices[i].datalink_name + "("
					+ _devices[i].datalink_description+")" );
			System.out.print( "    MAC address:");
			
			for ( byte b : _devices[i].mac_address )
				System.out.print( Integer.toHexString(b&0xff) + ":" );
			
			System.out.println();
			
			for ( NetworkInterfaceAddress a : _devices[i].addresses )
				System.out.println( "    address:" + a.address + " "
						+ a.subnet + " " + a.broadcast );
		}
		
		Scanner input = new Scanner( System.in );
		System.out.println( "\nPlease select which device your"
				+ " network of choice is connected to:" );
		
		int devNum = 0;
		boolean getInput = true;
		
		//Loop until a valid input is entered
		while ( getInput == true )
		{
			//Try to get the requested device
			try
			{
				devNum = input.nextInt();
				if ( devNum <= _devices.length && devNum >= 0 )
					getInput = false;
				else
					System.out.println( "Input a number corresponding"
							+ "to one of the listed devices." );
			}
			catch( InputMismatchException e )
			{
				System.out.println( "Invalid input please try again" );
				input = new Scanner( System.in );
			}
		}
		
		
		System.out.println( "Device: " + _devices[devNum].description + " selected." );
		//input.close();
		
		return _devices[devNum];
	}
}
